﻿#region license
//------------------------------------------------------------------------------
// <copyright company="Ascertaint.org">
//     Copyright (c) Ascertaint.org.  All rights reserved.
// </copyright> 
//
// Authors: Ming Tsai (ming@ascertaint.org)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//------------------------------------------------------------------------------
#endregion

using System;
using System.Web;

namespace Ums.CellPhoneInfo {
    /// <summary>
    /// Derives from CellPhoneParser, DefaultCellPhoneParser  is the catch-all parser that handles the regions that are not yet implemented.
    /// </summary>
    /// <reamrk>
    /// We implement Parser in per country/rule basis. if we cannot determine the region from the number to parse, DefaultCellPhoneParser
    /// is used.
    /// 
    /// As long the incoming numberToParse contains all numeric, we'll set the CellphoneNumber as valid.
    /// </remarks>
    public class DefaultCellPhoneParser : CellPhoneParser {

        protected override bool IsValid(string num) {
            CellPhoneNumber o = ParseExact(num);
            return o.IsValidNumber;
        }

        /// <summary>
        /// To pass the check, all you need is couple digitals of numerics..
        /// </summary>
        /// <param name="numToParse"></param>
        /// <returns></returns>
        protected override CellPhoneNumber ParseExact(string numToParse) {
            CellPhoneNumber o = new CellPhoneNumber();
            o.CountryIsoCode = "intl";

            //decode and strip off non numeric first
            //
            string num = HttpUtility.UrlDecode(numToParse);
            num = StriptOffNonNumeric(num);
            if (string.IsNullOrEmpty(num)) {
                o._isValidNumber = false;
                o.PhoneNumber = numToParse;
            }
            else {
                o.PhoneNumber = num;
                o._isValidNumber = true;
            }
            return o;
        }
    }
}